package com.wzl.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzl.backend.mapper.CommentMapper;
import com.wzl.backend.pojo.Comment;
import com.wzl.backend.pojo.User;
import com.wzl.backend.service.CommentService;
import com.wzl.backend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper , Comment>
                                implements CommentService {

    @Autowired
    UserService userService;

    @Override
    public List<Map<String, Object>> getByTopicId(String topicId) {
        List<Map<String, Object>> maps = new ArrayList<>();
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("topic_id" , topicId);
        List<Comment> list = list(queryWrapper);
        list.forEach(e -> {
            Map<String , Object> map = new HashMap<>();
            User user = userService.getById(e.getUId());
            map.put("data" , e);
            map.put("user" , user);
            maps.add(map);
        });
        return maps;
    }
}
